package com.ilaempire.bedb.client;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.TabPanel.TabPosition;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class TabCharacter extends TabItem{
	
	public void load(){
		setLayout(new BorderLayout());
		setText("Character");
		//tabCharacters BorderLayout
		BorderLayoutData WestData = new BorderLayoutData(LayoutRegion.WEST,150);
		BorderLayoutData CenterData = new BorderLayoutData(LayoutRegion.CENTER);
		WestData.setCollapsible(true);
		WestData.setSplit(true);
		ContentPanel West = new ContentPanel();
		LayoutContainer Center = new LayoutContainer();
		
		//tabWest AccordionLayout
		West.setLayout(new AccordionLayout());
		West.setHeading("ila");
		West.setBodyBorder(false);
		
		//tabWest -> Change
		ContentPanel WestChange = new ContentPanel();
		WestChange.setHeading("Character Selection");
		WestChange.setBorders(false);
		WestChange.setBodyBorder(false);
		West.add(WestChange);
		
		//tabWest -> Change -> Button Add Character From API
		Button AddCharacterFromAPI = new Button("Add Character", new SelectionListener<ButtonEvent>(){
			public void componentSelected(ButtonEvent ce){
				winAddCharacterFromAPI win = new winAddCharacterFromAPI();
				win.create();
			}
		});
		
		WestChange.add(AddCharacterFromAPI);
		
		//tabWest -> SkillTree
		ContentPanel WestSkillTree = new ContentPanel();
		WestSkillTree.setHeading("Skill Tree");
		WestSkillTree.setBorders(false);
		WestSkillTree.setBodyBorder(false);
		West.add(WestSkillTree);
		
		//tabCenter
		Center.setLayout(new FitLayout());
		Center.setBorders(false);
		
		//tabCenter -> SubTabs
		TabPanel subtabCharacter = new TabPanel();
		TabItem CenterSkill = new TabItem("Skill");
		TabItem CenterSummary = new TabItem("Summary");
		TabItem CenterAttribute = new TabItem("Attribute");
		TabItem CenterImplant = new TabItem("Implant");
		
		subtabCharacter.add(CenterSkill);
		subtabCharacter.add(CenterSummary);
		subtabCharacter.add(CenterAttribute);
		subtabCharacter.add(CenterImplant);
		subtabCharacter.setTabPosition(TabPosition.BOTTOM);
		Center.add(subtabCharacter);
		
		add(West,WestData);
		add(Center,CenterData);
		
	}
	
	class winAddCharacterFromAPI extends Dialog{
		private TextField<String> userID = new TextField<String>();
		private TextField<String> apiKey = new TextField<String>();
		
		public void create(){
			//Dialog
			setHeight(250);
			setWidth(300);
			setHeading("Enter API Information Here");
			setLayout(new FitLayout());
			//Form
			FormData formAddCharacterStep1Data = new FormData();
			FormPanel formAddCharacterStep1 = new FormPanel();
			//Label & Input
			userID.setFieldLabel("userID");
			apiKey.setFieldLabel("API KEY");
			userID.setValue("4703413");
			apiKey.setValue("A389A89B213246DAADE7C6883D930315887832AC09874DC58DBC49689E589AC1");
			//Button
			Button submit = new Button("Submit", new SelectionListener<ButtonEvent>(){
				public void componentSelected(ButtonEvent ce){
					jsonpAccountCharacter(userID.getValue(), apiKey.getValue());
					Info.display("Processing","Please Wait");
					hide();
				}
			});
			Button cancel = new Button("Cancel", new SelectionListener<ButtonEvent>(){
				public void componentSelected(ButtonEvent ce){
					hide();
				}
			});
			
			//Add
			formAddCharacterStep1.setLabelAlign(LabelAlign.TOP);
			formAddCharacterStep1.setButtonAlign(HorizontalAlignment.CENTER);
			formAddCharacterStep1.setBodyBorder(false);
			formAddCharacterStep1.add(userID,formAddCharacterStep1Data);
			formAddCharacterStep1.add(apiKey,formAddCharacterStep1Data);
			formAddCharacterStep1.add(submit);
			formAddCharacterStep1.add(cancel);
			formAddCharacterStep1.setHeaderVisible(false);
			
			add(formAddCharacterStep1);
			show();
			return;
		}
	}
	
	class winAddCharacterFromAPI2 extends Dialog {
		public void create(TabCharacterAccountCharacterData feed){
			setLayout(new FlowLayout());
			setHeight(250);
			setWidth(300);
			setHeading("Select Your Character");
			
			int charCount = feed.getCharacterNumber();
			for (int i = 0; i < charCount; ++i){
				add( createButton(feed.getName(i),feed.getCharacterID(i)) );
			}
		}
		
		private Button createButton(String characterName, String characterID){
			return new Button(characterName, new SelectionListener<ButtonEvent>(){
				public void componentSelected(ButtonEvent ce){
					//TO DO
				}
			});
		}
	}
	
	private void jsonpAccountCharacter(String userID, String apiKey){
		//String url = "http://localhost/bedb1/accountcharacter.php?userid=4703413&apikey=A389A89B213246DAADE7C6883D930315887832AC09874DC58DBC49689E589AC1";
		String page = "accountcharacter.php";
		String url = "http://localhost/bedb1/";
		//String url = "http://192.168.1.114:12345/bedb/bb_s_eve_database/ajax/";
		url = url + page + "?userid=" + userID + "&apikey=" + apiKey;
		JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
		jsonp.requestObject(url,
			new AsyncCallback<TabCharacterAccountCharacterData>() {
				public void onFailure(Throwable throwable){
					Info.display("Error","Error:" + throwable);
				}
				
				public void onSuccess(TabCharacterAccountCharacterData feed){
					//Info.display("JSONP",feed.getName(0) + ";" + feed.getName(1) + ";" + feed.getCharacterNumber());
					winAddCharacterFromAPI2 step2 = new winAddCharacterFromAPI2();
					step2.create(feed);
					step2.show();
				}
			}
		);
	}
}
